//
//  JLCityResultController.m
//  Liang美团HD
//
//  Created by sun on 16/1/23.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLCityResultController.h"
#import "JLData.h"
#import "JLCities.h"
@interface JLCityResultController ()
@property(nonatomic,strong)NSArray * cityResult;
@end

@implementation JLCityResultController
static NSString * const ID =@"cities";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
-(void)setSearchText:(NSString *)searchText{

    _searchText = [searchText copy];
    
    self.cityResult = [JLData citiesWithName:searchText.lowercaseString];
    [self.tableView reloadData];
    
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.cityResult.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
    JLCities * cities = self.cityResult[indexPath.row];
    cell.textLabel.text = cities.name;
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [NSString stringWithFormat:@"一共搜索到了%zd条结果",self.cityResult.count];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:JLCityDidChangeNotification object:self.cityResult[indexPath.row]];
}
@end
